//
//  HWUpdateAppView.m
//  嗨文
//
//  Created by 响当当 on 7/1/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "HWUpdateAppView.h"

static HWUpdateAppView *alertUpDateView = nil;

@interface HWUpdateAppView()
@property (nonatomic, strong)UIView *bgView;
@property (nonatomic, strong)UILabel *contextLabel;
@property (nonatomic, strong)UIButton *cancelBtn;
@end

@implementation HWUpdateAppView

+ (HWUpdateAppView *)alertUpDateView
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!alertUpDateView) {
            CGRect frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
            alertUpDateView = [[HWUpdateAppView alloc]initWithFrame:frame];
        }
    });
    return alertUpDateView;
}

- (UIView *)bgView
{
    if (!_bgView)
    {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(36.5*kUIScaleBasePlus,122.5*kUIScaleBasePlus,302*kUIScaleBasePlus,400*kUIScaleBasePlus)];
        _bgView.backgroundColor = [UIColor clearColor];
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(36.5*kUIScaleBasePlus,122.5*kUIScaleBasePlus,302*kUIScaleBasePlus,400*kUIScaleBasePlus)];
        imageView.image = [UIImage imageNamed:@"update_bg"];
        [_bgView addSubview:imageView];
    
        _contextLabel = [Utilties creatLabelWithFrame:CGRectMake(22*kUIScaleBasePlus,232*kUIScaleBasePlus,295*kUIScaleBasePlus,90*kUIScaleBasePlus)
                                         textAligment:NSTextAlignmentLeft
                                             textClor:LCHexColor(0xA5A5A5)
                                             textFont:12*kUIScaleBasePlus];
        _contextLabel.numberOfLines = 0;
        [_bgView addSubview:_contextLabel];
        
        _cancelBtn = [Utilties creatButtonWithFrame:CGRectMake(SCREEN_WIDTH/2-20*kUIScaleBasePlus,CGRectGetMaxY(self.bgView.frame)+34.5*kUIScaleBasePlus,40*kUIScaleBasePlus,40*kUIScaleBasePlus)
                                       textAligment:UIControlContentHorizontalAlignmentCenter
                                               text:@""
                                           textClor:[UIColor whiteColor]
                                          buttonBGC:[UIColor clearColor]
                                           textFont:16*kUIScaleBasePlus
                                             target:self
                                             method:@selector(cancel)];
        _cancelBtn.hidden = YES;
        [self addSubview:_cancelBtn];
        
        
        
        
        UIButton *btn = [Utilties creatButtonWithFrame:CGRectMake(47*kUIScaleBasePlus,_bgView.frame.size.height-68*kUIScaleBasePlus,200*kUIScaleBasePlus,48*kUIScaleBasePlus)
                                            textAligment:UIControlContentHorizontalAlignmentCenter
                                                    text:@"立即更新"
                                                textClor:[UIColor whiteColor]
                                               buttonBGC:JSAMainColor
                                                textFont:23*kUIScaleBasePlus
                                                  target:self
                                                  method:@selector(updateApp)];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = btn.frame.size.height/2;
        [_bgView addSubview:btn];
    }
    return _bgView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.75];
        [self addSubview:self.bgView];
        
    }
    return self;
}


- (void)updateApp
{
    DISMISS_SVP(1)
    LCUserDefaultsSet(@"0", @"updateVersonCount");
    LCUserDefaultsSyn;
    NSString *url = @"itms-apps://itunes.apple.com/cn/app/id1476808250?mt=8";
    [Utilties openUrlSchedule:url];
}

- (void)cancel
{
    if (self.cacnelViewBlock)
    {
        
        LCUserDefaultsSet(@"0", @"updateVersonCount");
        LCUserDefaultsSyn;
        
        
        current_User.detail = @"";
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [current_User savaSessionSucess:^{
                self.cacnelViewBlock();
                [self removeFromSuperview];
            } fail:^(NSError *error) {
                
            }];
        }];
        
       
    }
}

- (void)setModel:(UpdateModel *)model
{
    _model = model;
    self.contextLabel.text = model.detail;
    [self.contextLabel sizeToFit];
    
    if (model.isNew.integerValue == 1)
    {
        self.cancelBtn.hidden = YES;
    }
    else
    {
        self.cancelBtn.hidden = NO;
        [self.cancelBtn setImage:[UIImage imageNamed:@"update_close"] forState:UIControlStateNormal];
    }
}

@end
